package com.framework.app.controller.login;

import com.framework.app.entity.dto.LoginDTO;
import com.framework.app.entity.vo.AppMemberLoginVO;
import com.framework.app.service.IAppMemberLoginService;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.core.util.StpAppUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 移动用户登录管理
 *
 * @author Kevin
 */
@RestController
@RequestMapping("/app")
@AllArgsConstructor
public class AppMemberLoginController {

    private IAppMemberLoginService appUserService;

    /**
     * 登录
     *
     * @param req
     * @return
     */
    @PostMapping("/login")
    @AutoLog(module = "授权管理", title = "登录", isSaveRequestData = false)
    public R<AppMemberLoginVO> login(@Validated @RequestBody LoginDTO req) {
        String token = appUserService.login(req);
        AppMemberLoginVO loginVO = new AppMemberLoginVO();
        loginVO.setToken(token);
        return R.success(loginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @AutoLog(module = "授权管理", title = "退出")
    public R<Void> logout() {
        try {
            StpAppUtil.logout();
        } catch (Exception ex) {
        }
        return R.success();
    }

}
